<template>
  <fs-page>
    <fs-crud ref="crudRef" v-bind="crudBinding" custom-class="resource-crud">
      <div>
        <el-button style="margin-left: 20px" @click="addHandle({})">
          <fs-icon :icon="ui.icons.add"></fs-icon>
          添加</el-button
        >
      </div>
      <fs-permission-tree
        class="permission-tree"
        :tree="crudBinding.data"
        :editable="true"
        :checkable="false"
        :actions="permission"
        :default-expand-all="true"
        @add="addHandle"
        @edit="editHandle"
        @remove="removeHandle"
      ></fs-permission-tree>
    </fs-crud>
  </fs-page>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { useCrud, useExpose, CrudExpose, useUi } from "@fast-crud/fast-crud";
import createCrudOptions from "./crud";
import FsPermissionTree from "./fs-permission-tree.vue";
export default defineComponent({
  name: "AuthorityResource",
  components: { FsPermissionTree },
  setup() {
    // crud组件的ref
    const crudRef = ref();
    // crud 配置的ref
    const crudBinding = ref();
    // 暴露的方法
    const exposeRet: { expose: CrudExpose } = useExpose({ crudRef, crudBinding });
    const expose: CrudExpose = exposeRet.expose;
    // 你的crud配置
    const { crudOptions } = createCrudOptions({ expose });
    // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
    // 初始化crud配置
    // 此处传入permission进行通用按钮权限设置，会通过commonOptions去设置actionbar和rowHandle的按钮的show属性
    // 更多关于按钮权限的源代码设置，请参考 ./src/plugin/fast-crud/index.js （75-77行）
    const { resetCrudOptions } = useCrud({ expose, crudOptions });
    // 你可以调用此方法，重新初始化crud配置
    // resetCrudOptions(options)

    // 页面打开后获取列表数据
    onMounted(async () => {
      await expose.doRefresh();
    });

    //用户业务代码

    async function addHandle(item) {
      await expose.openAdd({ row: { parentId: item?.id ?? null } });
    }
    async function editHandle(item) {
      await expose.openEdit({ row: item });
    }
    async function removeHandle(item) {
      await expose.doRemove({ row: { id: item.id } });
    }

    const permission = ref({
      add: true,
      edit: true,
      remove: true
    });

    const { ui } = useUi();
    return {
      crudBinding,
      crudRef,
      addHandle,
      editHandle,
      removeHandle,
      permission,
      ui
    };
  }
});
</script>
<style lang="less">
.permission-tree {
  margin-left: 20px;
  margin-right: 20px;
  flex: 1;
}


.resource-crud{
  .fs-container .box .inner .body{
    display: flex;
    flex-direction: column;
  }
}
</style>
